<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pseudorandom.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_pseudorandom.js.html">pseudorandom.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview PseudoRandom provides a mechanism for generating deterministic
<a name="line17"></a> * psuedo random numbers based on a seed. Based on the Park-Miller algorithm.
<a name="line18"></a> * See http://dx.doi.org/10.1145%2F63039.63042 for details.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.testing.PseudoRandom&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Class for unit testing code that uses Math.random. Generates deterministic
<a name="line30"></a> * random numbers.
<a name="line31"></a> *
<a name="line32"></a> * @param {number=} opt_seed The seed to use.
<a name="line33"></a> * @param {boolean=} opt_install Whether to install the PseudoRandom at
<a name="line34"></a> *     construction time.
<a name="line35"></a> * @extends {goog.Disposable}
<a name="line36"></a> * @constructor
<a name="line37"></a> * @final
<a name="line38"></a> */
<a name="line39"></a>goog.testing.PseudoRandom = function(opt_seed, opt_install) {
<a name="line40"></a>  goog.Disposable.call(this);
<a name="line41"></a>
<a name="line42"></a>  if (!goog.isDef(opt_seed)) {
<a name="line43"></a>    opt_seed = goog.testing.PseudoRandom.seedUniquifier_++ + goog.now();
<a name="line44"></a>  }
<a name="line45"></a>  this.seed(opt_seed);
<a name="line46"></a>
<a name="line47"></a>  if (opt_install) {
<a name="line48"></a>    this.install();
<a name="line49"></a>  }
<a name="line50"></a>};
<a name="line51"></a>goog.inherits(goog.testing.PseudoRandom, goog.Disposable);
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * Helps create a unique seed.
<a name="line56"></a> * @type {number}
<a name="line57"></a> * @private
<a name="line58"></a> */
<a name="line59"></a>goog.testing.PseudoRandom.seedUniquifier_ = 0;
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Constant used as part of the algorithm.
<a name="line64"></a> * @type {number}
<a name="line65"></a> */
<a name="line66"></a>goog.testing.PseudoRandom.A = 48271;
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>/**
<a name="line70"></a> * Constant used as part of the algorithm. 2^31 - 1.
<a name="line71"></a> * @type {number}
<a name="line72"></a> */
<a name="line73"></a>goog.testing.PseudoRandom.M = 2147483647;
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Constant used as part of the algorithm. It is equal to M / A.
<a name="line78"></a> * @type {number}
<a name="line79"></a> */
<a name="line80"></a>goog.testing.PseudoRandom.Q = 44488;
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Constant used as part of the algorithm. It is equal to M % A.
<a name="line85"></a> * @type {number}
<a name="line86"></a> */
<a name="line87"></a>goog.testing.PseudoRandom.R = 3399;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Constant used as part of the algorithm to get values from range [0, 1).
<a name="line92"></a> * @type {number}
<a name="line93"></a> */
<a name="line94"></a>goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE =
<a name="line95"></a>    1.0 / (goog.testing.PseudoRandom.M - 1);
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * The seed of the random sequence and also the next returned value (before
<a name="line100"></a> * normalization). Must be between 1 and M - 1 (inclusive).
<a name="line101"></a> * @type {number}
<a name="line102"></a> * @private
<a name="line103"></a> */
<a name="line104"></a>goog.testing.PseudoRandom.prototype.seed_ = 1;
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Whether this PseudoRandom has been installed.
<a name="line109"></a> * @type {boolean}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.testing.PseudoRandom.prototype.installed_;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * The original Math.random function.
<a name="line117"></a> * @type {function(): number}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.testing.PseudoRandom.prototype.mathRandom_;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Installs this PseudoRandom as the system number generator.
<a name="line125"></a> */
<a name="line126"></a>goog.testing.PseudoRandom.prototype.install = function() {
<a name="line127"></a>  if (!this.installed_) {
<a name="line128"></a>    this.mathRandom_ = Math.random;
<a name="line129"></a>    Math.random = goog.bind(this.random, this);
<a name="line130"></a>    this.installed_ = true;
<a name="line131"></a>  }
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/** @override */
<a name="line136"></a>goog.testing.PseudoRandom.prototype.disposeInternal = function() {
<a name="line137"></a>  goog.testing.PseudoRandom.superClass_.disposeInternal.call(this);
<a name="line138"></a>  this.uninstall();
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Uninstalls the PseudoRandom.
<a name="line144"></a> */
<a name="line145"></a>goog.testing.PseudoRandom.prototype.uninstall = function() {
<a name="line146"></a>  if (this.installed_) {
<a name="line147"></a>    Math.random = this.mathRandom_;
<a name="line148"></a>    this.installed_ = false;
<a name="line149"></a>  }
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Seed the generator.
<a name="line155"></a> *
<a name="line156"></a> * @param {number=} seed The seed to use.
<a name="line157"></a> */
<a name="line158"></a>goog.testing.PseudoRandom.prototype.seed = function(seed) {
<a name="line159"></a>  this.seed_ = seed % (goog.testing.PseudoRandom.M - 1);
<a name="line160"></a>  if (this.seed_ &lt;= 0) {
<a name="line161"></a>    this.seed_ += goog.testing.PseudoRandom.M - 1;
<a name="line162"></a>  }
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * @return {number} The next number in the sequence.
<a name="line168"></a> */
<a name="line169"></a>goog.testing.PseudoRandom.prototype.random = function() {
<a name="line170"></a>  var hi = Math.floor(this.seed_ / goog.testing.PseudoRandom.Q);
<a name="line171"></a>  var lo = this.seed_ % goog.testing.PseudoRandom.Q;
<a name="line172"></a>  var test = goog.testing.PseudoRandom.A * lo -
<a name="line173"></a>             goog.testing.PseudoRandom.R * hi;
<a name="line174"></a>  if (test &gt; 0) {
<a name="line175"></a>    this.seed_ = test;
<a name="line176"></a>  } else {
<a name="line177"></a>    this.seed_ = test + goog.testing.PseudoRandom.M;
<a name="line178"></a>  }
<a name="line179"></a>  return (this.seed_ - 1) * goog.testing.PseudoRandom.ONE_OVER_M_MINUS_ONE;
<a name="line180"></a>};
</pre>


</body>
</html>
